# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'productManagment.ui'
#
# Created: Fri Jun 01 01:44:19 2012
#      by: PyQt4 UI code generator 4.9.1
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
from pl.edu.budgetanalizer.GUI.databaseManagment.products import EditProduct
from pl.edu.budgetanalizer.model.Product import Product

import pyodbc

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s
    
class Ui_Form(QtGui.QWidget):
    
    listOfProducts = []
    
    def getAllCategories(self,cur):
        self.categoryComboBox.clear()
        
        cur.execute("select * from Categories")
        rows = cur.fetchall()
        for row in rows:
            self.categoryComboBox.addItem(row.name)
            
    def getAllProducers(self,cur):
        self.producentComboBox.clear()
        
        cur.execute("select * from Producers")
        rows = cur.fetchall()
        for row in rows:
            self.producentComboBox.addItem(row.name)
            
    def getAllMeasures(self,cur):
        self.measureComboBox.clear()
        
        cur.execute("select * from Measures")
        rows = cur.fetchall()
        for row in rows:
            self.measureComboBox.addItem(row.name)
    
    def getAllProducts(self):
        '''
        Pobiera wszystkie produkty z bazy
        '''
        self.listOfMeasures = []
        self.deleteComboBox.clear()
        self.editComboBox.clear()
        
        conn = pyodbc.connect(self.databaseDir)
        cur = conn.cursor()
        cur.execute("exec getProducts")
        rows = cur.fetchall()
        for row in rows:
            product = Product(row[0],row[1],row[2],row[3],row[4],row[5])
            self.listOfProducts.append(product)
            self.deleteComboBox.addItem(QtCore.QString(product.name + ', ' + product.producer))
            self.editComboBox.addItem(QtCore.QString(product.name + ', ' + product.producer))
        conn.close()
    
    def setAllComoboBoxes(self):
        conn = pyodbc.connect(self.databaseDir)
        cur = conn.cursor()
        self.getAllCategories(cur)
        self.getAllMeasures(cur)
        self.getAllProducers(cur)
        conn.close()
        
    def delete(self):
        '''
        usuwa dany produkt z bazy
        '''
        conn = pyodbc.connect(self.databaseDir)
        cur = conn.cursor()
        s = str(self.deleteComboBox.currentText())
        name, producer = s.split(', ')
        try:
            cur.execute("exec deleteProductByName ?, ? ",name, producer)
            conn.commit()
        except pyodbc.IntegrityError:
            dialog = QtGui.QDialog()
            dialog.resize(358, 132)
            dialog.setGeometry(QtCore.QRect(50,50,100,100))
            label = QtGui.QLabel(dialog)
            label.setText("Nie mozna dokonac tej operacji - blad integracji")
            dialog.show()
            
        conn.close()
        self.getAllProducts()

    
    def add(self):
        '''
        dodaje nowy produkt do bazy
        '''
        conn = pyodbc.connect(self.databaseDir)
        cur = conn.cursor()
        cur.execute("exec getProducer ?",str(self.producentComboBox.currentText()))
        row = cur.fetchone()
        if row:
            id_producer = row[0]
        cur.execute("exec getCategory ?",str(self.categoryComboBox.currentText()))
        row = cur.fetchone()
        if row:
            id_category = row[0]
        cur.execute("exec getMeasure ?",str(self.measureComboBox.currentText()))
        row = cur.fetchone()
        if row:
            id_measure = row[0]
        cur.execute("exec addName ?",str(self.nameLineEdit.text()))
        cur.execute("exec getName ?",str(self.nameLineEdit.text()))
        row = cur.fetchone()
        if row:
            id_name = row[0]
        cur.execute("exec addProduct ?,?,?,?,?,?",id_name,id_producer,id_category,id_measure,float(self.quantityLineEdit.text()),float(self.priceLineEdit.text()))
        conn.commit()
        conn.close()
        self.nameLineEdit.setText("")
        self.quantityLineEdit.setText("")
        self.priceLineEdit.setText("")
        self.getAllProducts()
        
    def openEdit(self):
        s = str(self.editComboBox.currentText())
        name, producer = s.split(', ')
        self.productManagmentWidget = EditProduct.Ui_Form()
        self.productManagmentWidget.setupUi(self.productManagmentWidget,name,producer,self.Form,self.databaseDir)
        self.productManagmentWidget.setGeometry(QtCore.QRect(0, 0, 400, 120))
        self.productManagmentWidget.setAutoFillBackground(True)
        self.productManagmentWidget.setGeometry(QtCore.QRect(440,280,420,280))
        self.productManagmentWidget.show()
        
    
    def setupUi(self, Form,databaseDir):
        self.Form = Form
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(406, 411)
        self.databaseDir = databaseDir
        
        #add
        self.addLabel = QtGui.QLabel(Form)
        self.addLabel.setGeometry(QtCore.QRect(20, 20, 361, 21))
        self.addLabel.setObjectName(_fromUtf8("addLabel"))
        self.addButton = QtGui.QPushButton(Form)
        self.addButton.setGeometry(QtCore.QRect(310, 200, 75, 23))
        self.addButton.setObjectName(_fromUtf8("addButton"))
        self.addButton.connect(self.addButton, QtCore.SIGNAL("clicked()"),self.add)
        
         
        self.gridLayoutWidget = QtGui.QWidget(Form)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(20, 50, 371, 141))
        self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
        self.addLayout = QtGui.QGridLayout(self.gridLayoutWidget)
        self.addLayout.setMargin(0)
        self.addLayout.setObjectName(_fromUtf8("addLayout"))
        self.nameLabel = QtGui.QLabel(self.gridLayoutWidget)
        self.nameLabel.setObjectName(_fromUtf8("nameLabel"))
        self.addLayout.addWidget(self.nameLabel, 0, 0, 1, 1)
        self.nameLineEdit = QtGui.QLineEdit(self.gridLayoutWidget)
        self.nameLineEdit.setObjectName(_fromUtf8("nameLineEdit"))
        self.addLayout.addWidget(self.nameLineEdit, 1, 0, 1, 1)
        self.quantityLabel = QtGui.QLabel(self.gridLayoutWidget)
        self.quantityLabel.setObjectName(_fromUtf8("quantityLabel"))
        self.addLayout.addWidget(self.quantityLabel, 0, 1, 1, 1)
        self.producerLabel = QtGui.QLabel(self.gridLayoutWidget)
        self.producerLabel.setObjectName(_fromUtf8("producerLabel"))
        self.addLayout.addWidget(self.producerLabel, 2, 0, 1, 1)
        self.producentComboBox = QtGui.QComboBox(self.gridLayoutWidget)
        self.producentComboBox.setObjectName(_fromUtf8("producentComboBox"))
        self.addLayout.addWidget(self.producentComboBox, 3, 0, 1, 1)
        self.categoryLabel = QtGui.QLabel(self.gridLayoutWidget)
        self.categoryLabel.setObjectName(_fromUtf8("categoryLabel"))
        self.addLayout.addWidget(self.categoryLabel, 4, 0, 1, 1)
        self.categoryComboBox = QtGui.QComboBox(self.gridLayoutWidget)
        self.categoryComboBox.setObjectName(_fromUtf8("categoryComboBox"))
        self.addLayout.addWidget(self.categoryComboBox, 5, 0, 1, 1)
        self.priceLabel = QtGui.QLabel(self.gridLayoutWidget)
        self.priceLabel.setObjectName(_fromUtf8("priceLabel"))
        self.addLayout.addWidget(self.priceLabel, 2, 1, 1, 1)
        self.quantityLineEdit = QtGui.QLineEdit(self.gridLayoutWidget)
        self.quantityLineEdit.setObjectName(_fromUtf8("quantityLineEdit"))
        self.addLayout.addWidget(self.quantityLineEdit, 1, 1, 1, 1)
        self.priceLineEdit = QtGui.QLineEdit(self.gridLayoutWidget)
        self.priceLineEdit.setObjectName(_fromUtf8("priceLineEdit"))
        self.addLayout.addWidget(self.priceLineEdit, 3, 1, 1, 1)
        self.measureComboBox = QtGui.QComboBox(self.gridLayoutWidget)
        self.measureComboBox.setObjectName(_fromUtf8("measureComboBox"))
        self.addLayout.addWidget(self.measureComboBox, 5, 1, 1, 1)
        self.measureLabel = QtGui.QLabel(self.gridLayoutWidget)
        self.measureLabel.setObjectName(_fromUtf8("measureLabel"))
        self.addLayout.addWidget(self.measureLabel, 4, 1, 1, 1)
        
        #edit
        self.editLabel = QtGui.QLabel(Form)
        self.editLabel.setGeometry(QtCore.QRect(20, 260, 361, 21))
        self.editLabel.setObjectName(_fromUtf8("editLabel"))
        self.editComboBox = QtGui.QComboBox(Form)
        self.editComboBox.setGeometry(QtCore.QRect(20, 290, 211, 22))
        self.editComboBox.setObjectName(_fromUtf8("editComboBox"))
        self.editButton = QtGui.QPushButton(Form)
        self.editButton.setGeometry(QtCore.QRect(310, 290, 75, 23))
        self.editButton.setObjectName(_fromUtf8("editButton"))
        self.deleteComboBox = QtGui.QComboBox(Form)
        self.editButton.connect(self.editButton, QtCore.SIGNAL("clicked()"),self.openEdit)
        
        #delete
        self.deleteLabel = QtGui.QLabel(Form)
        self.deleteLabel.setGeometry(QtCore.QRect(20, 340, 361, 21))
        self.deleteLabel.setObjectName(_fromUtf8("deleteLabel"))
        self.deleteComboBox.setGeometry(QtCore.QRect(20, 370, 211, 22))
        self.deleteComboBox.setObjectName(_fromUtf8("deleteComboBox"))
        self.deleteButton = QtGui.QPushButton(Form)
        self.deleteButton.setGeometry(QtCore.QRect(310, 370, 75, 23))
        self.deleteButton.setObjectName(_fromUtf8("deleteButton"))
        self.deleteButton.connect(self.deleteButton, QtCore.SIGNAL("clicked()"),self.delete)

        self.getAllProducts()
        self.setAllComoboBoxes()
               
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.addLabel.setText(QtGui.QApplication.translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">Dodaj nowy produkt</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.editLabel.setText(QtGui.QApplication.translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">Edytuj istniejacy produkt:</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.deleteLabel.setText(QtGui.QApplication.translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">Usun istniejacy produkt</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.addButton.setText(QtGui.QApplication.translate("Form", "Dodaj", None, QtGui.QApplication.UnicodeUTF8))
        self.editButton.setText(QtGui.QApplication.translate("Form", "Edytuj", None, QtGui.QApplication.UnicodeUTF8))
        self.deleteButton.setText(QtGui.QApplication.translate("Form", "Usuń", None, QtGui.QApplication.UnicodeUTF8))
        self.nameLabel.setText(QtGui.QApplication.translate("Form", "Nazwa:", None, QtGui.QApplication.UnicodeUTF8))
        self.quantityLabel.setText(QtGui.QApplication.translate("Form", "Ilosc:", None, QtGui.QApplication.UnicodeUTF8))
        self.producerLabel.setText(QtGui.QApplication.translate("Form", "Producent:", None, QtGui.QApplication.UnicodeUTF8))
        self.categoryLabel.setText(QtGui.QApplication.translate("Form", "Kategoria:", None, QtGui.QApplication.UnicodeUTF8))
        self.priceLabel.setText(QtGui.QApplication.translate("Form", "Cena:", None, QtGui.QApplication.UnicodeUTF8))
        self.measureLabel.setText(QtGui.QApplication.translate("Form", "Jednostka:", None, QtGui.QApplication.UnicodeUTF8))

